//
//  DsTouxiangYoujiCell.m
//  Tour
//
//  Created by 张张烨 on 16/3/10.
//  Copyright © 2016年 张中烨. All rights reserved.
//

#import "DsTouxiangYoujiCell.h"
#define WIDTH1 self.contentView.frame.size.width
#define HEIGHT self.contentView.frame.size.height
@interface DsTouxiangYoujiCell ()<UIScrollViewDelegate>
@property (nonatomic, retain)UIImageView *picture;
@property (nonatomic, retain) BaseLabel *title;
@property (nonatomic, retain) BaseLabel *time;
@property (retain, nonatomic) UIScrollView *scrollView;
@property (retain, nonatomic) UIImageView *lastImageView;
@property (nonatomic, assign)CGRect originalFrame;
@property (nonatomic, assign)BOOL isDoubleTap;
@end

@implementation DsTouxiangYoujiCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.picture = [[UIImageView alloc]init];
        [self.contentView addSubview:_picture];
        
        
        self.time = [[BaseLabel alloc]init];
        _time.textColor = [UIColor whiteColor];
        [self.contentView addSubview:_time];
      
        
        self.title = [[BaseLabel alloc]init];
        _title.numberOfLines = 0;
        _title.textColor = [UIColor whiteColor];
        [self.contentView addSubview:_title];
       
#pragma mark - 点击图片放大,在点击还原
//        // 开启用户交互
//        self.picture.userInteractionEnabled = YES;
//        //添加单击手势
//        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showZoomImageView:)];
//        
//        [_picture addGestureRecognizer:tap];

    }
    return self;
}
#pragma mark - 点击图片放大,在点击还原
-(void)showZoomImageView:(UITapGestureRecognizer *)tap
{
    if (![(UIImageView *)tap.view image]) {
        return;
    }
    //scrollView作为背景
    UIScrollView *bgView = [[UIScrollView alloc] init];
    bgView.frame = [UIScreen mainScreen].bounds;
    bgView.backgroundColor = [UIColor blackColor];
    UITapGestureRecognizer *tapBg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBgView:)];
    [bgView addGestureRecognizer:tapBg];
    
    UIImageView *picView = (UIImageView *)tap.view;
    
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = picView.image;
    imageView.frame = [bgView convertRect:picView.frame fromView:self.contentView];
    [bgView addSubview:imageView];
    
    [[[UIApplication sharedApplication] keyWindow] addSubview:bgView];
    
    self.lastImageView = imageView;
    self.originalFrame = imageView.frame;
    self.scrollView = bgView;
    //最大放大比例
    self.scrollView.maximumZoomScale = 1.5;
    self.scrollView.delegate = self;
    
    [UIView animateWithDuration:0.5 animations:^{
        CGRect frame = imageView.frame;
        frame.size.width = bgView.frame.size.width;
        frame.size.height = frame.size.width * (imageView.image.size.height / imageView.image.size.width);
        frame.origin.x = 0;
        frame.origin.y = (bgView.frame.size.height - frame.size.height) * 0.5;
        imageView.frame = frame;
    }];
}

-(void)tapBgView:(UITapGestureRecognizer *)tapBgRecognizer
{
    self.scrollView.contentOffset = CGPointZero;
    [UIView animateWithDuration:0.5 animations:^{
        self.lastImageView.frame = self.originalFrame;
        tapBgRecognizer.view.backgroundColor = [UIColor clearColor];
    } completion:^(BOOL finished) {
        [tapBgRecognizer.view removeFromSuperview];
        self.scrollView = nil;
        self.lastImageView = nil;
    }];
}

//返回可缩放的视图
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.lastImageView;
}


-(void)setModel:(DsTouxiangModel *)model
{
    _model = model;
    
    [_picture sd_setImageWithURL:[NSURL URLWithString:_model.front_cover_photo_url]];
    _time.text = _model.start_date;
    _title.text = _model.name;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    _picture.frame = CGRectMake(10 * KUAN, 10 * GAO, WIDTH1 - 20, HEIGHT - 15);
    _title.frame = CGRectMake(25 * KUAN, 20 * GAO, WIDTH1 - (50 * KUAN), 40 * GAO);
    _time.frame = CGRectMake(30 * KUAN, 60 * GAO, WIDTH1 - (50 * KUAN ), 20 *GAO);
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
